<template>
  <el-dialog
    :title="!dataForm.couponId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="优惠券名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="优惠券名称"></el-input>
    </el-form-item>
    <el-form-item label="使用开始时间" prop="startTime">
      <el-input v-model="dataForm.startTime" placeholder="使用开始时间"></el-input>
    </el-form-item>
    <el-form-item label="使用结束时间" prop="endTime">
      <el-input v-model="dataForm.endTime" placeholder="使用结束时间"></el-input>
    </el-form-item>
    <el-form-item label="金额" prop="amount">
      <el-input v-model="dataForm.amount" placeholder="金额"></el-input>
    </el-form-item>
    <el-form-item label="领取上限" prop="limitNum">
      <el-input v-model="dataForm.limitNum" placeholder="领取上限"></el-input>
    </el-form-item>
    <el-form-item label="类型：1 本店、2 全网" prop="chantType">
      <el-input v-model="dataForm.chantType" placeholder="类型：1 本店、2 全网"></el-input>
    </el-form-item>
    <el-form-item label="优惠券类型：1 店铺 2 指定商品 3 指定类目" prop="couponType">
      <el-input v-model="dataForm.couponType" placeholder="优惠券类型：1 店铺 2 指定商品 3 指定类目"></el-input>
    </el-form-item>
    <el-form-item label="使用门槛金额" prop="moneyLimit">
      <el-input v-model="dataForm.moneyLimit" placeholder="使用门槛金额"></el-input>
    </el-form-item>
    <el-form-item label="是否仅抵扣邮费：0 否、1 是" prop="flag">
      <el-input v-model="dataForm.flag" placeholder="是否仅抵扣邮费：0 否、1 是"></el-input>
    </el-form-item>
    <el-form-item label="优惠券描述信息" prop="couponDesc">
      <el-input v-model="dataForm.couponDesc" placeholder="优惠券描述信息"></el-input>
    </el-form-item>
    <el-form-item label="发放总数" prop="totalNum">
      <el-input v-model="dataForm.totalNum" placeholder="发放总数"></el-input>
    </el-form-item>
    <el-form-item label="剩余数量" prop="stockNum">
      <el-input v-model="dataForm.stockNum" placeholder="剩余数量"></el-input>
    </el-form-item>
    <el-form-item label="创建者ID" prop="creator">
      <el-input v-model="dataForm.creator" placeholder="创建者ID"></el-input>
    </el-form-item>
    <el-form-item label="商户ID,-1表示全网" prop="chantId">
      <el-input v-model="dataForm.chantId" placeholder="商户ID,-1表示全网"></el-input>
    </el-form-item>
    <el-form-item label="推广渠道ID,-1表示线上全网" prop="channelId">
      <el-input v-model="dataForm.channelId" placeholder="推广渠道ID,-1表示线上全网"></el-input>
    </el-form-item>
    <el-form-item label="活动状态：1 可领取、0 已结束、-1 删除" prop="status">
      <el-input v-model="dataForm.status" placeholder="活动状态：1 可领取、0 已结束、-1 删除"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="modifyTime">
      <el-input v-model="dataForm.modifyTime" placeholder="修改时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          couponId: 0,
          name: '',
          startTime: '',
          endTime: '',
          amount: '',
          limitNum: '',
          chantType: '',
          couponType: '',
          moneyLimit: '',
          flag: '',
          couponDesc: '',
          totalNum: '',
          stockNum: '',
          creator: '',
          chantId: '',
          channelId: '',
          status: '',
          createTime: '',
          modifyTime: ''
        },
        dataRule: {
          name: [
            { required: true, message: '优惠券名称不能为空', trigger: 'blur' }
          ],
          startTime: [
            { required: true, message: '使用开始时间不能为空', trigger: 'blur' }
          ],
          endTime: [
            { required: true, message: '使用结束时间不能为空', trigger: 'blur' }
          ],
          amount: [
            { required: true, message: '金额不能为空', trigger: 'blur' }
          ],
          limitNum: [
            { required: true, message: '领取上限不能为空', trigger: 'blur' }
          ],
          chantType: [
            { required: true, message: '类型：1 本店、2 全网不能为空', trigger: 'blur' }
          ],
          couponType: [
            { required: true, message: '优惠券类型：1 店铺 2 指定商品 3 指定类目不能为空', trigger: 'blur' }
          ],
          moneyLimit: [
            { required: true, message: '使用门槛金额不能为空', trigger: 'blur' }
          ],
          flag: [
            { required: true, message: '是否仅抵扣邮费：0 否、1 是不能为空', trigger: 'blur' }
          ],
          couponDesc: [
            { required: true, message: '优惠券描述信息不能为空', trigger: 'blur' }
          ],
          totalNum: [
            { required: true, message: '发放总数不能为空', trigger: 'blur' }
          ],
          stockNum: [
            { required: true, message: '剩余数量不能为空', trigger: 'blur' }
          ],
          creator: [
            { required: true, message: '创建者ID不能为空', trigger: 'blur' }
          ],
          chantId: [
            { required: true, message: '商户ID,-1表示全网不能为空', trigger: 'blur' }
          ],
          channelId: [
            { required: true, message: '推广渠道ID,-1表示线上全网不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '活动状态：1 可领取、0 已结束、-1 删除不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          modifyTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.couponId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.couponId) {
            this.$http({
              url: this.$http.adornUrl(`/coupon/mercouponinfo/info/${this.dataForm.couponId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.merCouponInfo.name
                this.dataForm.startTime = data.merCouponInfo.startTime
                this.dataForm.endTime = data.merCouponInfo.endTime
                this.dataForm.amount = data.merCouponInfo.amount
                this.dataForm.limitNum = data.merCouponInfo.limitNum
                this.dataForm.chantType = data.merCouponInfo.chantType
                this.dataForm.couponType = data.merCouponInfo.couponType
                this.dataForm.moneyLimit = data.merCouponInfo.moneyLimit
                this.dataForm.flag = data.merCouponInfo.flag
                this.dataForm.couponDesc = data.merCouponInfo.couponDesc
                this.dataForm.totalNum = data.merCouponInfo.totalNum
                this.dataForm.stockNum = data.merCouponInfo.stockNum
                this.dataForm.creator = data.merCouponInfo.creator
                this.dataForm.chantId = data.merCouponInfo.chantId
                this.dataForm.channelId = data.merCouponInfo.channelId
                this.dataForm.status = data.merCouponInfo.status
                this.dataForm.createTime = data.merCouponInfo.createTime
                this.dataForm.modifyTime = data.merCouponInfo.modifyTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/coupon/mercouponinfo/${!this.dataForm.couponId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'couponId': this.dataForm.couponId || undefined,
                'name': this.dataForm.name,
                'startTime': this.dataForm.startTime,
                'endTime': this.dataForm.endTime,
                'amount': this.dataForm.amount,
                'limitNum': this.dataForm.limitNum,
                'chantType': this.dataForm.chantType,
                'couponType': this.dataForm.couponType,
                'moneyLimit': this.dataForm.moneyLimit,
                'flag': this.dataForm.flag,
                'couponDesc': this.dataForm.couponDesc,
                'totalNum': this.dataForm.totalNum,
                'stockNum': this.dataForm.stockNum,
                'creator': this.dataForm.creator,
                'chantId': this.dataForm.chantId,
                'channelId': this.dataForm.channelId,
                'status': this.dataForm.status,
                'createTime': this.dataForm.createTime,
                'modifyTime': this.dataForm.modifyTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
